package leecode

func letterCombinations(digits string) (result []string) {
	if len(digits) == 0 {
		return
	}
	wordMap := make(map[int]string, 8)
	wordMap[2] = "abc"
	wordMap[3] = "def"
	wordMap[4] = "ghi"
	wordMap[5] = "jkl"
	wordMap[6] = "mno"
	wordMap[7] = "pqrs"
	wordMap[8] = "tuv"
	wordMap[9] = "wxyz"

	var backtracking func(startIndex int)
	path := make([]byte, 0, len(digits))
	backtracking = func(startIndex int) {
		if len(path) == len(digits) {
			temp := string(path)
			result = append(result, temp)
			return
		}
		digit := int(digits[startIndex] - '0') // 将index指向的数字转为int（确定下一个数字）
		//从map集合中取出数据
		str := wordMap[digit]
		for i := 0; i < len(str); i++ {
			path = append(path, str[i])
			backtracking(startIndex + 1)
			path = path[:len(path)-1]
		}
	}
	backtracking(0)

	return
}
